package com.mkuspis.moviebrowser.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.mkuspis.moviebrowser.MovieBrowserApplication;
import com.mkuspis.moviebrowser.R;
import com.mkuspis.moviebrowser.activities.MovieDetailsActivity;
import com.mkuspis.moviebrowser.datamanagment.ISubscriber;
import com.mkuspis.moviebrowser.datamanagment.datamanagers.MovieListDataManager;
import com.mkuspis.moviebrowser.datamanagment.models.Movie;

import java.util.ArrayList;

/**
 * Fragment with movie list data.
 */
public class MovieListFragment extends Fragment implements ISubscriber {

    public ArrayList<Movie> movieEntities;
    public int position;

    private ListViewAdapter adapter;
    private MovieListDataManager movieListDataManager;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        position = getArguments().getInt("pos");
        movieListDataManager = ((MovieBrowserApplication) getActivity().getApplication())
                .getMovieListDataManager();
    }

    @Override
    public void onStart() {
        movieListDataManager.onSubscribe(this);
        movieListDataManager.getData(this);
        movieListDataManager.getPosters(this);

        super.onStart();
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             final ViewGroup container,
                             final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_movie_list, null, false);
    }

    @Override
    public void onViewCreated(final View view,
                              final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    public void onUpdateView(final ArrayList<Movie> films) {
        movieEntities = films;

        final ListView listView = (ListView) getView().findViewById(android.R.id.list);
        setOnListItemClickListener(listView);

        adapter = new ListViewAdapter(getActivity(), R.layout.item, movieEntities);
        listView.setAdapter(adapter);
    }

    private void setOnListItemClickListener(final ListView listView) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent,
                                    final View view,
                                    final int position,
                                    final long id) {
                final Intent intent = new Intent(getActivity(), MovieDetailsActivity.class);
                int movieID = movieEntities.get(position).getMovieID();
                intent.putExtra(MovieDetailsActivity.MOVIE_ID_KEY, movieID);
                startActivity(intent);
            }
        });
    }

    public void onUpdateImage(final String localPath,
                              final int pos) {
        adapter.getItem(pos).setLocalPosterPath(localPath);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onStop() {
        movieListDataManager.onUnsubscribe(this);
        super.onDestroy();
    }

    private static class ListViewAdapter extends ArrayAdapter<Movie> {

        private final ArrayList<Movie> objects;

        private ListViewAdapter(final Context context,
                                final int textViewResourceId,
                                final ArrayList<Movie> objects) {
            super(context, textViewResourceId, objects);
            this.objects = objects;
        }

        public View getView(final int position,
                            final View convertView,
                            final ViewGroup parent) {

            View view = convertView;

            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.item, null, false);
            }

            final Movie movie = objects.get(position);

            if (movie != null) {
                final ImageView poster = (ImageView) view.findViewById(R.id.poster);
                final ProgressBar spinner = (ProgressBar) view.findViewById(R.id.listImageSpinner);
                final TextView title = (TextView) view.findViewById(R.id.title);
                final TextView votes = (TextView) view.findViewById(R.id.voteCount);
                final TextView releaseDate = (TextView) view.findViewById(R.id.releaseDate);
                final RatingBar ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);

                if (movie.getLocalPosterPath() == null) {
                    poster.setVisibility(View.INVISIBLE);
                    spinner.setVisibility(View.VISIBLE);
                } else if (movie.getLocalPosterPath().equals(getContext().getString(R.string.no_image))) {
                    poster.setImageResource(R.drawable.no_image);
                    poster.setVisibility(View.VISIBLE);
                    spinner.setVisibility(View.INVISIBLE);
                } else if (movie.getLocalPosterPath() != null) {
                    poster.setImageURI(Uri.parse(movie.getLocalPosterPath()));
                    poster.setVisibility(View.VISIBLE);
                    spinner.setVisibility(View.INVISIBLE);
                }

                title.setText(movie.getOriginalTitle());
                votes.setText(String.valueOf(movie.getVoteCount()));
                releaseDate.setText(movie.getReleaseDate());
                ratingBar.setRating(movie.getVoteAverage() / 2);
            }
            return view;
        }
    }
}